package org.example.everyday;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/5/13 0013 8:21
 */
public class Lc2530 {
    public static void main(String[] args) {
        Lc2530 lc2530 = new Lc2530();
        String first = "pae";
        String second = "ple";
        boolean res = lc2530.oneEditAway(first,second);
        System.out.println(res);

    }
    public boolean oneEditAway(String first, String second) {
        int m = first.length(), n = second.length();
        if(n - m == 1){
            return oneInsert(first,second);
        }else if(m - n == 1){
            return oneInsert(second,first);
        }else if(n == m){
            boolean foundDiff = false;
            for(int i = 0; i < m; i++){
                if(first.charAt(i) != second.charAt(i)){
                    if(!foundDiff){
                        foundDiff = true;
                    }else {
                        return false;
                    }
                }
            }
            return true;
        }else{
            return false;
        }
    }

    /**
     * 方法描述: 插入记录
     *
     * @param shorter .
     * @param longer .
     * @return boolean
     * @author 安路路
     * @date 2022/5/13 0013  8:24
     */
    public boolean oneInsert(String shorter, String longer){
        int len1 = shorter.length(), len2 = longer.length();
        int index1 = 0, index2 = 0;
        while (index1 < len1 && index2 < len2){
            if(shorter.charAt(index1) == longer.charAt(index2)){
                index1++;
            }
            index2++;
            if(index2 - index1 > 1){
                return false;
            }
        }
        return true;
    }
}
